package com.bwie.house.conmap;

/**
 * @author 龙
 * @version 1.0.0
 * @ClassName Coordinate.java
 * @Description TODO
 * @createTime 2022年05月12日 20:12:00
 */

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
* @Author: 龍
* @Description: 通过市+小区名称获取经纬度
* @DateTime: 2022/5/12 20:13
* @Params:
* @Return:
*/
public class Coordinate {
    public String api(String city,String address){
        String location="";
        String host = "http://geo.market.alicloudapi.com";
        String path = "/v3/geocode/geo";
        String method = "GET";
        String appcode = "489208f833cc412da21add31364c5efc";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("address", address);  //地址名称
        querys.put("city",city);  //市
        try {
            HttpResponse response = HttpUtilsAliyun.doGet(host, path, method, headers, querys);
            System.out.println("response："+response.toString());
            //获取response的body
            //获得JSON里面所有的值
            String str = EntityUtils.toString(response.getEntity());
            System.out.println("EntityUtils："+str);
            //获取JSON数组里面所有的值
            JSONObject jsonObject = JSONObject.parseObject(str);
            String s = jsonObject.get("geocodes").toString();
            System.out.println("geocodes："+s);
            //遍历geocodes的数组
            String json="";
            JSONArray geocodes = jsonObject.getJSONArray("geocodes");
            for (Object geocode : geocodes) {
                //JSON类型
                json=geocode.toString();
                System.out.println("json："+json);
            }
            //再通过JSON获取经纬度
            JSONObject object = JSONObject.parseObject(json);
            location = object.get("location").toString();
            System.out.println("location："+location);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return location;
    }
}
